//
//  ViewController.m
//  FFmpegDemo
//
//  Created by WZ_MAC on 2017/7/28.
//  Copyright © 2017年 wz. All rights reserved.
//

#import "ViewController.h"
#import "FFmpegDecoder.h"
#import "H264HwDecoderImpl.h"
#import "AAPLEAGLLayer.h"

#define     LYRtspLiveVideoPath           @"rtsp://192.168.1.254:554/xxx.mov" //30fps     //联咏视频,间隔录影(rtsp默认端口554)
#define     LYHttpLiveVideoPath           @"http://192.168.1.254:8192"        //25fps     //拍照,间隔拍照,全景,vr
#define     ABRtspLiveVideoPath           @"rtsp://192.168.42.1/live"         //29.97fps  //安霸实时流

@interface ViewController ()<H264HwDecoderImplDelegate>

@property (nonatomic, strong) FFmpegDecoder *ffmpegDecoder;

@property (nonatomic, strong) AAPLEAGLLayer *gllayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"沙盒地址:%@",NSHomeDirectory());

}

- (IBAction)start:(id)sender {

    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    _gllayer = [[AAPLEAGLLayer alloc] initWithFrame:CGRectMake(0, 30, screenW, screenW * 360/640)];
    _gllayer.backgroundColor = [UIColor blackColor].CGColor;
    [self.view.layer addSublayer:_gllayer];
    
    H264HwDecoderImpl *h264decoder = [[H264HwDecoderImpl alloc] init];
    h264decoder.delegate = self;
    
    _ffmpegDecoder = [FFmpegDecoder createDecoderWithPath:LYRtspLiveVideoPath error:nil];
    _ffmpegDecoder.h264Decoder = h264decoder;
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
        
        NSLog(@"这里是子线程吗？:%@",[NSThread currentThread]);
        [_ffmpegDecoder startReadAVPacketAndDecodeNalu]; //必须异步解码
        NSLog(@"子线程while循环结束了!startReadAVPacketAndDecodeNalu方法终于返回了!:%@",[NSThread currentThread]);
    });
    
    NSLog(@"打印了这个，说明主线程未被阻塞！:%@",[NSThread currentThread]);

    

    
}

- (IBAction)stop:(id)sender {
    
    [_gllayer removeFromSuperlayer];
    _gllayer = nil;
    [_ffmpegDecoder stopReadAVPacketAndDecodeNalu];
    _ffmpegDecoder = nil;
}


#pragma mark ============ ffmpegDecoder代理 ==============
- (void)displayDecodedSourceFrameRef:(void *)srcFrameRefCon cvImageBufferRef:(CVImageBufferRef)imageBuffer
{
    NSLog(@"硬解码成功啦!线程:%@",[NSThread currentThread]);
    _gllayer.pixelBuffer = imageBuffer;
    
}


/*
 [mjpeg @ 0x146877400] Format mjpeg detected only with low score of 25, misdetection possible!
 Input #0, mjpeg, from 'http://192.168.1.254:8192'
 Duration: N/A, bitrate: N/A
 Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 640x480, 25 tbr, 1200k tbn, 25 tbc
 
 
 
 Input #0, rtsp, from 'rtsp://192.168.1.254:554/xxx.mov':
 Metadata:
 title           : Nvt RTSP, streamed by the LIVE555 Media Server
 comment         : stream0/svc0/
 Duration: N/A, start: 0.200000, bitrate: N/A
 Stream #0:0: Video: h264 (High), yuv420p, 640x360, 30 fps, 30 tbr, 90k tbn, 60 tbc
 [h264 @ 0x7fdf14065e00] Packet header is not contained in global extradata, corrupted stream or invalid MP4/AVCC bitstream
 
 
 
 Input #0, rtsp, from 'rtsp://192.168.42.1/live':
 Metadata:
 title           : Ambarella streaming
 comment         : Ambarella streaming
 Duration: N/A, start: 0.323322, bitrate: N/A
 Stream #0:0: Video: h264 (Main), yuv420p(tv, bt709), 1280x720, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
 [h264 @ 0x7f980d81a400] Packet header is not contained in global extradata, corrupted stream or invalid MP4/AVCC bitstream
 
 
 */

@end
